
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Andik Taufiq
 * Each tile represents each number of puzzle
 * March, 27 2010 - Bandung, Indonesia
 */
public class Tile {
    public int x, y, width, height;
    public int type, position;
    private Image tile1;
    private Image tile2;
    private Image tile3;
    private Image tile4;
    private Image tile5;
    private Image tile6;
    private Image tile7;
    private Image tile8;
    private Board board;

    public Tile(Board board, int type, int position) {
        this.board = board;
        this.type = type;
        this.position = position;

        try {
            tile1		= Image.createImage("/tile1.png");
            tile2		= Image.createImage("/tile2.png");
            tile3		= Image.createImage("/tile3.png");
            tile4		= Image.createImage("/tile4.png");
            tile5		= Image.createImage("/tile5.png");
            tile6		= Image.createImage("/tile6.png");
            tile7		= Image.createImage("/tile7.png");
            tile8		= Image.createImage("/tile8.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        width = tile1.getWidth();
        height = tile1.getHeight();
        x = this.board.xTilesRef;
        y = this.board.yTilesRef;
    }

	public void setXY(int x, int y) {
		this.x = this.board.xTilesRef;
		this.y = this.board.yTilesRef;
	}

    public void paint(Graphics g) {
		this.x = this.board.xTilesRef;
		this.y = this.board.yTilesRef;
        switch (type) {
            case 0:
            break;
            case 1:
                switch (position) {
                    case 0:
                        g.drawImage(tile1, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile1, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile1, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile1, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile1, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile1, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile1, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile1, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile1, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 2:
                switch (position) {
                    case 0:
                        g.drawImage(tile2, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile2, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile2, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile2, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile2, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile2, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile2, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile2, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile2, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 3:
                switch (position) {
                    case 0:
                        g.drawImage(tile3, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile3, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile3, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile3, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile3, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile3, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile3, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile3, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile3, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 4:
                switch (position) {
                    case 0:
                        g.drawImage(tile4, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile4, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile4, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile4, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile4, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile4, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile4, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile4, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile4, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 5:
                switch (position) {
                    case 0:
                        g.drawImage(tile5, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile5, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile5, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile5, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile5, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile5, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile5, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile5, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile5, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 6:
                switch (position) {
                    case 0:
                        g.drawImage(tile6, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile6, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile6, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile6, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile6, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile6, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile6, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile6, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile6, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 7:
                switch (position) {
                    case 0:
                        g.drawImage(tile7, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile7, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile7, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile7, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile7, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile7, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile7, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile7, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile7, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
            case 8:
                switch (position) {
                    case 0:
                        g.drawImage(tile8, board.xTilesRef, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 1:
                        g.drawImage(tile8, board.xTilesRef + width + 3, board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 2:
                        g.drawImage(tile8, board.xTilesRef + ((width + 3) * 2), board.yTilesRef, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 3:
                        g.drawImage(tile8, board.xTilesRef, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 4:
                        g.drawImage(tile8, board.xTilesRef + width + 3, board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 5:
                        g.drawImage(tile8, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + height + 3, Graphics.TOP | Graphics.LEFT);
                    break;
                    case 6:
                        g.drawImage(tile8, board.xTilesRef, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 7:
                        g.drawImage(tile8, board.xTilesRef + width + 3, board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                    case 8:
                        g.drawImage(tile8, board.xTilesRef + ((width + 3) * 2), board.yTilesRef + ((height + 3) * 2), Graphics.TOP | Graphics.LEFT);
                    break;
                }
            break;
        }
    }
}
